package org.myOrg.spetrum.GUI;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

import org.myOrg.spetrum.io.InputManager;
import org.myOrg.spetrum.spectrumStruct.SpectrumFactory;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;
import org.myOrg.spetrum.util.Time;
import org.myOrg.spetrum.util.Transform;

public class ConsolPanel extends ShowPanel implements Reciver {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ConsolPanel() {
		this.register(MessageMangementImplement.GETINPUTPATH, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.READOVER, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.RUNOVER, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.RUN, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.ADDBUGMODEL, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.DELETEBUGMODEL, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.CHANGEALOGRITHM, OverallFactory
				.getMessageManger());
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.GETINPUTPATH) {
			this.insertOneLine("user choose the file:"
					+ ((String) msg.getInfo()), StyleSet.getStyleConsleRight());
		} else if (msg.getType() == MessageMangementImplement.RUN) {
			this.insertOneLine("the program is running", StyleSet
					.getStyleConsleRight());
		} else if (msg.getType() == MessageMangementImplement.RUNOVER) {
			this.insertOneLine("run over ", StyleSet.getStyleConsleRight());
		} else if (msg.getType() == MessageMangementImplement.READOVER) {
			InputManager input = (InputManager) msg.getInfo();
			this.insertOneLine(input.getFormatlInfo(),
					(input.isFormatPass() ? StyleSet.getStyleConsleRight()
							: StyleSet.getStyleConsleWrong()));
		} else if (msg.getType() == MessageMangementImplement.ADDBUGMODEL) {
			String bug = Transform.vectorToString((int[]) msg.getInfo());
			this.insertString("user insert the bug: ", StyleSet
					.getStyleConsleRight());
			this.insertString(bug, StyleSet.getStyleMode());
			this.insertOneLine("", StyleSet.getStyleConsleRight());
		}
		else if(msg.getType() == MessageMangementImplement.DELETEBUGMODEL){
			this.insertOneLine("user delete the "+ (Integer)msg.getInfo()+"th bug", StyleSet.getStyleConsleRight());
		}
		else if(msg.getType() == MessageMangementImplement.CHANGEALOGRITHM){
			int type = ((Integer)msg.getInfo()).intValue();
			if(type < SpectrumFactory.ALORGRITMNUM)
			this.insertOneLine("user choose the "+SpectrumFactory.getName(type)+" alogrithm", StyleSet.getStyleConsleRight());
			else 
				this.insertOneLine("user choose compare  all the alogrithm ", StyleSet.getStyleConsleRight());
			
		}
	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

	public void insertOneLine(String string, AttributeSet style) {
		try {
			doc.insertString(doc.getLength(), string + " "
					+ Time.getCurrentTime() + "\r\n", style);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
